﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;

namespace XCT.Forms
{
    public partial class FormDangNhap : DevExpress.XtraEditors.XtraForm
    {
        public FormDangNhap()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormDangNhap(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region "Sự Kiện"

        private void FormDangNhap_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    btnExit_Click(sender, null);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        
        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)
            {
                txtPass.Text = "";
                txtPass.Focus();
            }
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                txtUser.Text = "";
                txtUser.Focus();
            }
            else if (e.KeyCode == Keys.Return)
                btnLogin_Click(sender, null);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (clsDungChung.strUser == "")
            {
                Application.Exit();
            }
            else
            {
                this.Close();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            clsDangNhap clsdn = new clsDangNhap();
            clsDungChung clsdc = new clsDungChung();
            clsdn.User = txtUser.Text;
            clsdn.Pass = txtPass.Text;
                        
            SqlDataReader dr;
            dr = clsdn.DocNguoiDung_XCT_TheoMaNV();
            if (dr.Read() == true)
            {
                if ((string)dr["Pass"] == clsdn.Pass)
                {
                    formMain.SetBarLogout(clsDungChung.strBar);
                    formMain.CloseFromsActive();

                    clsDungChung.strUser = (string)dr["MaNV"];
                    clsDungChung.strBar = (string)dr["Quyen"];
                    clsDungChung.strBar = clsDungChung.strBar.TrimEnd('.');

                    formMain.SetBarLogin(clsDungChung.strBar);
                    formMain.SetUsersLogin(clsdc.ConvertVNI2Unicode((string)dr["TenNV"]));
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show("Mật khẩu không đúng !", "Thông Báo:", MessageBoxButtons.OK);
                    txtPass.Text = "";
                    txtPass.Focus();
                }
            }
            else
            {
                XtraMessageBox.Show("Tên đăng nhập không đúng !", "Thông Báo:", MessageBoxButtons.OK);
                txtPass.Text = "";
                txtPass.Focus();
            }
        }

        #endregion
    }
}